unit F_DocumentSearchResults;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ZAbstractRODataset, ZDataset, StdCtrls, Buttons,
  JvExControls, JvSpeedButton, Grids, DBGrids, ExtCtrls, JvExDBGrids,
  JvDBGrid, JvDBUltimGrid, Math;
{$WARNINGS OFF}
type
  TDocument_Search_Results = class(TForm)
    Panel_Principal: TPanel;
    Panel_Inferior: TPanel;
    BB_Cancelar: TBitBtn;
    BB_Seleccionar: TBitBtn;
    Q_Buscar: TZReadOnlyQuery;
    DS_Buscar: TDataSource;
    JvDBUltimGrid: TJvDBUltimGrid;
    procedure BB_CancelarClick(Sender: TObject);
    procedure BB_SeleccionarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    constructor Create (var owner: TComponent; ID_Busqueda:integer; TextoBusqueda, idCarrera:string);

    procedure ParseDelimited(const sl : TStrings; const value : string; const delimiter : string) ;

    procedure BuscarSinOrdenSubString(Texto, idCarrera: String);
    procedure BuscarEmpiezaPor(Texto, idCarrera: String);
    procedure BuscarExacta(Texto, idCarrera: String);

    procedure SetOptimalDBGridCellWidth(var sg: TJvDBUltimGrid; Table: TDataSet);
    function countCharacters(Text:String):integer;

    procedure EstablecerApariencia;
  end;
{$WARNINGS ON}
var
  Document_Search_Results: TDocument_Search_Results;

implementation

uses DM_Persistencia;

{$R *.dfm}
constructor TDocument_Search_Results.Create (var owner: TComponent; ID_Busqueda:integer; TextoBusqueda, idCarrera:string);
begin
  inherited Create(owner);
  case ID_Busqueda of
    0: BuscarEmpiezaPor(TextoBusqueda, idCarrera);
    1: BuscarSinOrdenSubString(TextoBusqueda, idCarrera);
    2: BuscarExacta(TextoBusqueda, idCarrera);
  end;
  Caption:= Caption + UpperCase('"'+TextoBusqueda+'"');
end;

procedure TDocument_Search_Results.EstablecerApariencia;
begin

end;

procedure TDocument_Search_Results.ParseDelimited(const sl : TStrings; const value : string; const delimiter : string) ;
var
   dx : integer;
   ns : string;
   txt : string;
   delta : integer;
begin
   delta := Length(delimiter) ;
   txt := value + delimiter;
   sl.BeginUpdate;
   sl.Clear;
   try
     while Length(txt) > 0 do
     begin
       dx := Pos(delimiter, txt) ;
       ns := Copy(txt,0,dx-1) ;
       sl.Add(ns) ;
       txt := Copy(txt,dx+delta,MaxInt) ;
     end;
   finally
     sl.EndUpdate;
   end;
end;

procedure TDocument_Search_Results.BuscarSinOrdenSubString(Texto, idCarrera: String);
var
  ListaDeBusqueda: TStrings;
  i:integer;
begin
  i:=0;
  with Q_Buscar do
  begin
    Close;
    SQL.Clear;
    ListaDeBusqueda := TStringList.Create;
    try
      SQL.Append('SELECT DOCUMENTOS.ID as ID_DOCUMENTO, '+'MATERIAS_POR_CARRERA.FK_IDCARRERA as ID_CARRERA, MATERIAS.ID AS ID_MATERIA, MATERIAS_POR_CARRERA.ANIO, MATERIAS_POR_CARRERA.CUATRIMESTRE, DOCUMENTOS.TITULO, DOCUMENTOS.DESCRIPCION, MATERIAS.DENOMINACION, CARRERAS.NOMBRE AS '+'NOMBRE_CARRERA FROM DOCUMENTOS  JOIN DOCUMENTOS_POR_MATERIA ON DOCUMENTOS.ID = DOCUMENTOS_POR_MATERIA.FK_ID_DOCUMENTO  JOIN MATERIAS ON MATERIAS.ID = DOCUMENTOS_POR_MATERIA.FK_ID_MATERIA JOIN '+'MATERIAS_POR_CARRERA on MATERIAS_POR_CARRERA.FK_IDMATERIA = MATERIAS.ID JOIN CARRERAS ON CARRERAS.ID = MATERIAS_POR_CARRERA.FK_IDCARRERA');
      ParseDelimited(ListaDeBusqueda, Texto, ' ') ;
      SQL.Append('WHERE DOCUMENTOS.TITULO CONTAINING '+QuotedStr(ListaDeBusqueda.Strings[i]));
      for i:= 1 to ListaDeBusqueda.Count - 1 do
      begin
        SQL.Append('AND DOCUMENTOS.TITULO CONTAINING '+QuotedStr(ListaDeBusqueda.Strings[i]));
      end;
        SQL.Append(' ORDER BY DOCUMENTOS.TITULO');
        Open;
    finally
      ListaDeBusqueda.Free;
    end;
  end;
end;

procedure TDocument_Search_Results.BuscarEmpiezaPor(Texto, idCarrera: String);
var
  s:String;
begin
  s:= 'SELECT DOCUMENTOS.ID as ID_DOCUMENTO, '+'MATERIAS_POR_CARRERA.FK_IDCARRERA as ID_CARRERA, MATERIAS.ID AS ID_MATERIA, MATERIAS_POR_CARRERA.ANIO, MATERIAS_POR_CARRERA.CUATRIMESTRE, DOCUMENTOS.TITULO, DOCUMENTOS.DESCRIPCION, MATERIAS.DENOMINACION, CARRERAS.NOMBRE AS '+'NOMBRE_CARRERA FROM DOCUMENTOS  JOIN DOCUMENTOS_POR_MATERIA ON DOCUMENTOS.ID = DOCUMENTOS_POR_MATERIA.FK_ID_DOCUMENTO  JOIN MATERIAS ON MATERIAS.ID = DOCUMENTOS_POR_MATERIA.FK_ID_MATERIA JOIN '+'MATERIAS_POR_CARRERA on MATERIAS_POR_CARRERA.FK_IDMATERIA = MATERIAS.ID JOIN CARRERAS ON CARRERAS.ID = MATERIAS_POR_CARRERA.FK_IDCARRERA WHERE UPPER (DOCUMENTOS.TITULO) STARTING WITH '+QuotedStr(UpperCase(Texto))+' ORDER BY DOCUMENTOS.TITULO';
  with Q_Buscar do
    begin
      SQL.Clear;
      Close;
      SQL.Add(s);
      Open;
    end;
end;

procedure TDocument_Search_Results.BuscarExacta(Texto, idCarrera: String);
var
  s:String;
begin
  s:= 'SELECT DOCUMENTOS.ID as ID_DOCUMENTO, '+'MATERIAS_POR_CARRERA.FK_IDCARRERA as ID_CARRERA, MATERIAS.ID AS ID_MATERIA, MATERIAS_POR_CARRERA.ANIO, MATERIAS_POR_CARRERA.CUATRIMESTRE, DOCUMENTOS.TITULO, DOCUMENTOS.DESCRIPCION, MATERIAS.DENOMINACION, CARRERAS.NOMBRE AS '+'NOMBRE_CARRERA FROM DOCUMENTOS  JOIN DOCUMENTOS_POR_MATERIA ON DOCUMENTOS.ID = DOCUMENTOS_POR_MATERIA.FK_ID_DOCUMENTO  JOIN MATERIAS ON MATERIAS.ID = DOCUMENTOS_POR_MATERIA.FK_ID_MATERIA JOIN '+'MATERIAS_POR_CARRERA on MATERIAS_POR_CARRERA.FK_IDMATERIA = MATERIAS.ID JOIN CARRERAS ON CARRERAS.ID = MATERIAS_POR_CARRERA.FK_IDCARRERA WHERE UPPER (DOCUMENTOS.TITULO) = '+QuotedStr(UpperCase(Texto))+' ORDER BY DOCUMENTOS.TITULO';
  with Q_Buscar do
    begin
      SQL.Clear;
      Close;
      SQL.Add(s);
      Open;
    end;
end;

procedure TDocument_Search_Results.SetOptimalDBGridCellWidth(var sg: TJvDBUltimGrid; Table: TDataSet);
 const
  DEF_ROW_SIZE=21;
 var
   i, count : Integer;
   max_width, max_height: Integer;
 begin
   max_height:= DEF_ROW_SIZE;
   with sg do
   begin
     Canvas.Font.Assign(Font);
     for i := 0 to (Columns.Count - 1) do
     begin
       max_width := Canvas.TextWidth(SG.Columns[i].Title.Caption)+2; {Para que el valor minimo sea el del titulo}
       Table.First;
       While not Table.Eof do
         begin
           max_width := Math.Max(max_width,Canvas.TextWidth(Table.FieldByName(Fields[i].FieldName).AsString))+2;
//           count:= countCharacters(Table.FieldByName(Fields[i].FieldName).AsString);
//           max_height := Math.Max(max_height, DEF_ROW_SIZE*count);
           Table.Next
         end;
       if max_width > 0 then
         Columns[I].Width := max_width + 4
       else
         Columns[I].Width := 20;//DefaultColWidth;
     end; { for }
//     sg.RowsHeight:=max_height;
   end;
   Table.First;
 end;

function TDocument_Search_Results.countCharacters(Text:String):integer;
var
  i, count:integer;
begin
  count:=1;
  if (Text<>'') then
    for i:=0 to Length(Text) do
      begin
        if (Text[i] = chr(13)) then
          count:= count+1;
      end;
  Result:= count;
end;

procedure TDocument_Search_Results.BB_CancelarClick(Sender: TObject);
begin
  ModalResult:= mrCancel;
end;

procedure TDocument_Search_Results.BB_SeleccionarClick(Sender: TObject);
begin
  ModalResult:= mrOk;
end;

end.
